package com.lovo.ssc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.csc.dto.PromotionDto;
import com.lovo.dto.CommodityDto;
import com.lovo.remoteservice.ICommodityService;
import com.lovo.ssc.entity.Seckill;
import com.lovo.ssc.entity.Shelves;
import com.lovo.ssc.service.ISeckillService;
import com.lovo.ssc.service.IShelvesService;
import com.lovo.sscsys.dto.SellKillGoodsDto;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: lf
 * @Date: 2022/7/7 21:13
 */
//用于接收审核系统返回的MQ
@Service
public class SellKillMQ {

    @Autowired
    ISeckillService seckillService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    IShelvesService shelvesService;

    @Autowired
    ICommodityService commodityService;

    @SneakyThrows
    @RabbitListener(queues = MQUtil.PromotionInfoQueue)
    public void sellKill(PromotionDto dto,
                         Channel channel,
                         @Header(AmqpHeaders.DELIVERY_TAG) long tag) {

//        System.out.println(dto);
        if (dto != null) {

            String goodsCode = dto.getPromotionInfoCode();
            QueryWrapper qw = new QueryWrapper();

            qw.eq("code", goodsCode);
            qw.eq("reason","申请促销");
            Seckill seckill = seckillService.getOne(qw);
            if (seckill == null){
                channel.basicAck(tag, false);
                return;
            }
            Float discount = seckill.getDiscount();
            QueryWrapper qw1 = new QueryWrapper( );

            qw1.eq("goodsCode", goodsCode);
            Shelves shelves = shelvesService.getOne(qw1);
            if (dto.getPurchaseInfo().equals("1")){
                shelves.setGoodspricce(shelves.getGoodspricce()*discount);
                shelves.setGoodsstate("1");
                shelvesService.saveOrUpdate(shelves);
                this.commodity();
            }else if (!dto.getPurchaseInfo().equals("1")){
                seckillService.remove(qw);
            }
            channel.basicAck(tag, false);
        }
    }

    @SneakyThrows
    @RabbitListener(queues = MQUtil.PromotionQueue)
    public void cancelSellKill(PromotionDto dto, Channel channel,
                               @Header(AmqpHeaders.DELIVERY_TAG) long tag){


//        System.out.println(dto);
        if (dto != null) {
            String goodsCode = dto.getPromotionInfoCode();

            QueryWrapper qw = new QueryWrapper();
            qw.eq("reason","申请取消促销");
            qw.eq("code", goodsCode);
            Seckill seckill = seckillService.getOne(qw);
            if (seckill == null){
                channel.basicAck(tag, false);
                return;
            }
            Float discount = seckill.getDiscount();
            QueryWrapper qw2 = new QueryWrapper();
            qw2.eq("goodsCode", goodsCode);
            Shelves shelves = shelvesService.getOne(qw2);
            if (dto.getPurchaseInfo().equals("1")){
                shelves.setGoodspricce(shelves.getGoodspricce()/discount);
                shelves.setGoodsstate("0");//0-已上架  1-促销中 2-预售商品 同意取消促销改为已上架
                shelvesService.saveOrUpdate(shelves);

                //克隆对象，传递到前端系统
//                List<CommodityDto> dtoList = new ArrayList<>();
//                CommodityDto commodityDto = new CommodityDto();
//                BeanUtils.copyProperties(shelves, commodityDto); //对象拷贝
//                dtoList.add(commodityDto);
//                commodityService.findAllCommodityBySscSys(dtoList);

                this.commodity();
            }else if (!dto.getPurchaseInfo().equals("1")){
                seckillService.remove(qw);
            }
            channel.basicAck(tag, false);
        }
    }

    public void commodity() {
        List<Shelves> list = shelvesService.list();
        List<CommodityDto> dtoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CommodityDto commodityDto = new CommodityDto();
            commodityDto.setCommoditycode(list.get(i).getGoodscode());
            commodityDto.setCommoditybatch(list.get(i).getGoodsbatch());
            commodityDto.setCommodityname(list.get(i).getGoodsname());
            commodityDto.setNorms(list.get(i).getGoodsspecs());
            commodityDto.setNumber(list.get(i).getNumber());
            commodityDto.setPrice(list.get(i).getGoodspricce());
            commodityDto.setState(list.get(i).getGoodsstate());
            commodityDto.setType(list.get(i).getGoodstype());
            dtoList.add(commodityDto);
        }
        commodityService.findAllCommodityBySscSys(dtoList);
    }

}

